﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FMS.Formulation.Licensing.Engine
{

    internal class LicensePath
    {
        public static string PathToCertificate()
        {
            string empty = string.Empty;
            string text = string.Empty;
            bool isTerminalServerInstalled = CheckTS.IsTerminalServerInstalled;
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string folderPath2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
            string folderPath3 = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            if (folderPath3.Length > 0)
            {
                text = Path.Combine(folderPath3, "Feed Management Systems");
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
                text = Path.Combine(text, "Formulation");
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
                text = Path.Combine(text, "Formulation.dlsc");
            }
            if (text.Length < 1 || !File.Exists(text))
            {
                if (SystemInformation.TerminalServerSession && isTerminalServerInstalled)
                {
                    empty = Path.Combine(folderPath, "Feed Management Systems\\Formulation.dlsc");
                    if (!SystemInformation.UserInteractive)
                    {
                        try
                        {
                            if (!File.Exists(empty))
                            {
                                empty = Path.Combine(folderPath2, "Feed Management Systems\\Formulation\\Formulation.dlsc");
                                if (!File.Exists(empty))
                                {
                                    empty = Path.Combine(folderPath, "Feed Management Systems\\Formulation.dlsc");
                                }
                            }
                        }
                        catch
                        {
                            empty = Path.Combine(folderPath, "Feed Management Systems\\Formulation.dlsc");
                        }
                    }
                }
                else
                {
                    empty = Path.Combine(folderPath2, "Feed Management Systems\\Formulation\\Formulation.dlsc");
                    if (!SystemInformation.UserInteractive)
                    {
                        try
                        {
                            if (!File.Exists(empty))
                            {
                                empty = Path.Combine(folderPath, "Feed Management Systems\\Formulation.dlsc");
                                if (!File.Exists(empty))
                                {
                                    empty = Path.Combine(folderPath2, "Feed Management Systems\\Formulation\\Formulation.dlsc");
                                }
                            }
                        }
                        catch
                        {
                            empty = Path.Combine(folderPath2, "Feed Management Systems\\Formulation\\Formulation.dlsc");
                        }
                    }
                }
                if (text.Length < 1)
                {
                    text = empty;
                }
                else
                {
                    try
                    {
                        if (File.Exists(empty))
                        {
                            try
                            {
                                File.Move(empty, text);
                            }
                            catch
                            {
                            }
                        }
                        string text2 = Path.Combine(Path.GetDirectoryName(empty), "LicenseConfiguration.xml");
                        string destFileName = Path.Combine(Path.GetDirectoryName(text), "LicenseConfiguration.xml");
                        if (File.Exists(text2))
                        {
                            try
                            {
                                File.Move(text2, destFileName);
                            }
                            catch
                            {
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return text;
        }
    }

}
